//
//  NSString+HX_MD5.m
//  UI24-加密（MD5）
//
//  Created by 韩旭 on 16/8/16.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "NSString+HX_MD5.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (HX_MD5)
- (NSString *)stringWithhx_MD5_32Bitmd_5Upper {
    return [[self stringWith32_Bit] uppercaseString];
}
- (NSString *)stringWithhx_MD5_32Bitmd_5Lower {
    return [[self stringWith32_Bit] lowercaseString];
}
- (NSString *)stringWith32_Bit {
    //   1. 将字符串转化成c语言
    const char *cString = [self UTF8String];
    //   2 将c的字符串转化_MD5
    CC_LONG lenth = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cString, lenth, bytes);
    //    3. 将C 字符串转化成OC的字符串
    NSMutableString *finalString = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i ++) {
        [finalString appendFormat:@"%02x", bytes[i]];
    }
    return finalString;
}

- (NSString *)stringWithhx_MD5_16 {
    NSString *string = [[self stringWith32_Bit] substringFromIndex:8];
    return [string substringToIndex:string.length - 8];
}
- (NSString *)stringWithhx_MD5_16Bitmd_5Lower {
    return [[self stringWithhx_MD5_16] lowercaseString];
}
- (NSString *)stringWithhx_MD5_16Bitmd_5Upper {
    return [[self stringWithhx_MD5_16] uppercaseString];
}




@end